/*
 * @Author: biankai
 * @Date: 2021-04-19 18:47:04
 * @LastEditTime: 2021-11-04 16:30:20
 * @LastEditors: biankai
 * @FilePath: /gitee/devops_service/src/main.ts
 */
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { HttpExceptionFilter } from './filters/http-exception.filter';
import { TransformInterceptor } from './interceptor/transform.interceptor';
import * as util from 'util';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
// 原生方法注入
const console_log = console.log;
console.log = (...objs: any[]): void => {
  for (const obj of objs) {
    console_log(`[${new Date().toLocaleString()}] - `, obj);
  }
};
console.debug = (...objs: any[]): void => {
  for (const obj of objs) {
    console.log(util.inspect(obj, true, 8, true));
  }
};
const port = Number(process.env.PORT || 8866);
async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());

  // 配个全局路由，便于nginx转发
  app.setGlobalPrefix('v1');
  // 全局注册错误的过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 全局注册拦截器
  app.useGlobalInterceptors(new TransformInterceptor());
  // 文档
  const options = new DocumentBuilder()
    .setTitle('Devops')
    .setDescription('基于 `Nest 7.0.1 版` 开发Devops接口')
    .setVersion('1.0')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('/v1/api-doc', app, document);

  await app.listen(port);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
